package com.yoda.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.youdianda.common.PageResult;
import com.yoda.youdianda.common.R;
import com.yoda.youdianda.entity.YddArticle;
import com.yoda.youdianda.entity.YddUser;
import com.yoda.youdianda.entity.YddUserData;
import com.yoda.youdianda.service.YddArticleService;
import com.yoda.youdianda.service.YddUserDataService;
import com.yoda.youdianda.service.YddUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author ManySisters
 * @create 2023-02-10 10:22
 */

/**
 * 会员管理
 */
@RestController
@Slf4j
@RequestMapping("/ydd_user")
public class YddUserController {
    @Autowired
    private YddUserService yddUserService;
    @Autowired
    private YddUserDataService yddUserDataService;
    @Autowired
    private YddArticleService yddArticleService;

    /**
     * 用户详情
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R rowInfo(Integer id){
        YddUser yddUser = yddUserService.getById(id);
        return R.success(yddUser);
    }

    /**
     * 用户列表
     *Request URL: ydd_user/list?sex=&username=&mobile=&page=1&limit=10
     * @param map
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam Map map) {
        //1、获取前端传来的数据
        String page = map.get("page").toString();
        String limit = map.get("limit").toString();
        String sex = (String) map.get("sex");
        String username = (String) map.get("username");
        String mobile = (String) map.get("mobile");

        //2.构造分页构造器
        Page<YddUser> pageInfo = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));

        //3.条件构造器
        LambdaQueryWrapper<YddUser> queryWrapper = new LambdaQueryWrapper<>();
        //4.添加查询条件
        queryWrapper.eq(sex != "", YddUser::getSex, sex);
        queryWrapper.like(username != "", YddUser::getUsername, username);
        queryWrapper.like(mobile != "", YddUser::getMobile, mobile);
        queryWrapper.orderByDesc(YddUser::getId);

        //执行查询
        yddUserService.page(pageInfo, queryWrapper);

        PageResult pageResult = new PageResult();
        pageResult.setData(pageInfo.getRecords());
        pageResult.setCount(pageInfo.getTotal());
        pageResult.setTotalPages((long) Math.ceil((double) pageInfo.getTotal() / pageInfo.getSize()));
        pageResult.setPageSize(pageInfo.getSize());
        pageResult.setCurrentPage(pageInfo.getCurrent());

        return R.success(pageResult);
    }

    /**
     * 添加用户
     * Request URL: /ydd_user/add
     * @param yddUser
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody YddUser yddUser) {
        yddUserService.save(yddUser);
        return R.success("添加成功");
    }

    /**
     * 用户删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    @Transactional
    public R del(int id) {
        YddUser yddUser = yddUserService.getById(id);
        Integer userId = yddUser.getId();//用户的id
        LambdaQueryWrapper<YddUserData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddUserData::getUserId, userId);
        yddUserDataService.remove(queryWrapper);//查点赞表,有则直接删除

        LambdaQueryWrapper<YddArticle> yddArticleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        yddArticleLambdaQueryWrapper.eq(YddArticle::getUserId, userId);
        yddArticleService.remove(yddArticleLambdaQueryWrapper);//删除文章表信息

        yddUserService.removeById(id);//删除用户信息
        return R.success("删除成功");
    }

    /**
     * 用户修改
     * @param yddUser
     * @return
     */
    @PostMapping("/update")
    @Transactional
    public R update(@RequestBody YddUser yddUser) {
        yddUserService.updateById(yddUser);

        LambdaQueryWrapper<YddArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddArticle::getUserId, yddUser.getId());
        List<YddArticle> list = yddArticleService.list(queryWrapper);
        if (list != null) {
            for (YddArticle yddArticle : list) {
                yddArticle.setAuthor(yddUser.getUsername());
                yddArticleService.updateById(yddArticle);
            }
        }
        return R.success("修改成功");
    }
}
